package net.ib.mtalk.ui.setting.user;

import java.util.UUID;

import net.ib.mntalk.protocol.Common;
import net.ib.mntalk.protocol.Common.AccessToken;
import net.ib.mntalk.protocol.Common.AccountAuthToken;
import net.ib.mntalk.protocol.Common.AccountId;
import net.ib.mntalk.protocol.Common.AccountInfo;
import net.ib.mntalk.protocol.Common.AccountToken;
import net.ib.mntalk.protocol.Common.EmailAccountInfo;
import net.ib.mntalk.protocol.Common.UUIDMsg;
import net.ib.mntalk.protocol.Session.AcquireAccountAuthTokenReq;
import net.ib.mntalk.protocol.Session.AcquireAccountTokenReq;
import net.ib.mntalk.protocol.Session.RegisterAccountReq;
import net.ib.mtalk.constant.MTalkConstant;
import net.ib.mtalk.global.GlobalPreferences;
import net.ib.mtalk.network.task.AcquireAccountAuthTask;
import net.ib.mtalk.network.task.AcquireAccountTask;
import net.ib.mtalk.network.task.RegisterAccountTask;
import net.ib.mtalk.ui.UIController;
import net.ib.mtalk.util.GpbSupport;
import android.content.Context;
import android.os.Handler;
import android.util.Log;

public class EmailController extends UIController{

	public EmailController(Context context, Handler handler) 
	{
		super(context, handler);
	}

	public void acquireAccountAuthToServer(String id) {
		AcquireAccountAuthTokenReq acquireAccountAuthTokenReq = new AcquireAccountAuthTokenReq();
		
		UUIDMsg appUuId = GpbSupport.makeUuidMsg(UUID.fromString("6fca4926-7558-4b09-ab05-6bb51bfd1eef"));
		
		String appSecret = "abcdefghijklmnopqrstuvwxyz";
		
		AccountInfo info = new AccountInfo();
		
		AccountId accountId = new AccountId();
		accountId.setType(Common.Email);
		accountId.setIdentifier(id);
		
		info.setId(accountId);
		
		EmailAccountInfo emailInfo = new EmailAccountInfo();
		emailInfo.setAddress(id);
		
		info.setEmail(emailInfo);
		
		acquireAccountAuthTokenReq.setAppUuid(appUuId);
		acquireAccountAuthTokenReq.setAppSecret(appSecret);
		acquireAccountAuthTokenReq.setAccountInfo(info);
		
		AcquireAccountAuthTask task = new AcquireAccountAuthTask(mContext, mUiHandler, acquireAccountAuthTokenReq);
		task.execute();
	}
	
	public void acquireAccountToServer(AccountAuthToken accountAuthToken) {
		AcquireAccountTokenReq aquireAccountTokenReq = new AcquireAccountTokenReq();
		aquireAccountTokenReq.setAccountAuthToken(accountAuthToken);
		
		AcquireAccountTask task = new AcquireAccountTask(mContext, mUiHandler, aquireAccountTokenReq);
		task.execute();
	}
	
	public void setUserAccount(AccountToken accountToken){
		try{
			RegisterAccountReq registerAccountReq = new RegisterAccountReq();
			registerAccountReq.setAccountToken(accountToken);
			registerAccountReq.setAccessToken(GlobalPreferences.getMessageFromPreferences(AccessToken.class));
			registerAccountReq.setUserUuid(GlobalPreferences.getUserUUID());
			
			RegisterAccountTask task = new RegisterAccountTask(mContext, mUiHandler, registerAccountReq);
			task.execute();
		} catch (Exception e) {
			Log.e(MTalkConstant.TAG, "setUserProfile", e);
		}
	}
}
